//
//  RewardView.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/6.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "RewardView.h"

#import "RewardCell.h"

#import "WBPopOverView.h"
#import "payChooseView.h"
@interface RewardView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>


@end

@implementation RewardView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createCollectionUI];
    }
    return self;
}

/**
 创建collectioview的布局
 */
-(void)createCollectionUI{
    self.collectionView.scrollsToTop = YES;
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.minimumLineSpacing = scaleTopAndBottom(10);
    self.flowLayout.minimumInteritemSpacing = scaleLeftAndRight(2);
//    self.collectionView.showsVerticalScrollIndicator = NO;
//    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.flowLayout.itemSize = CGSizeMake(scaleLeftAndRight(90), scaleTopAndBottom(130));

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
    [self addSubview:self.collectionView];
    [self.collectionView registerClass:[RewardCell class] forCellWithReuseIdentifier:@"RewardCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeader"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Collectionfooter"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 8;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RewardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RewardCell" forIndexPath:indexPath];
    cell.nameLabel.text = @"30元";
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *resusab = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionHeader" forIndexPath:indexPath];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"打赏详细";
        label.textAlignment = NSTextAlignmentRight;
        [resusab addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.with.equalTo(resusab).insets(UIEdgeInsetsMake(5, 5, 5, 5));
        }];
        return resusab;
    }
    else{
        UICollectionReusableView *resusabview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Collectionfooter" forIndexPath:indexPath];
        [self setSubviewsIn:resusabview];
        return resusabview;
    }
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(WIDTH, scaleTopAndBottom(35));
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(WIDTH, scaleTopAndBottom(50));
}
-(void)setSubviewsIn:(UICollectionReusableView *)resuableView{
    UILabel *tileLabel = [[UILabel alloc] init];
    tileLabel.text = @"需话费";
    [resuableView addSubview:tileLabel];
    [tileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(resuableView.mas_left).offset(scaleLeftAndRight(5));
        make.top.equalTo(resuableView.mas_top).offset(scaleTopAndBottom(0));
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(100), scaleTopAndBottom(20)));
    }];
    UIImageView *iconImageView = [[UIImageView alloc] init];
    [resuableView addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(resuableView.mas_left).offset(scaleLeftAndRight(5));
        make.top.equalTo(tileLabel.mas_bottom).offset(scaleTopAndBottom(5));
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(20), scaleTopAndBottom(20)));
    }];
    _costLabel = [[UILabel alloc] init];
    _costLabel.text = @"500元";
    [resuableView addSubview:_costLabel];
    [_costLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(scaleLeftAndRight(5));
        make.top.equalTo(tileLabel.mas_bottom).offset(scaleTopAndBottom(0));
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(120), scaleTopAndBottom(20)));
    }];
    _countBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    _countBtn.backgroundColor = [UIColor greenColor];
    [_countBtn setTitle:@"1个" forState:UIControlStateNormal];
    [_countBtn addTarget:self action:@selector(showCountList:) forControlEvents:UIControlEventTouchUpInside];
    [resuableView addSubview:_countBtn];
    UIButton *rewradBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rewradBtn.backgroundColor = [UIColor redColor];
    [rewradBtn setTitle:@"打赏" forState:UIControlStateNormal];
    [rewradBtn addTarget:self action:@selector(goPayChoose:) forControlEvents:UIControlEventTouchUpInside];
    [rewradBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [resuableView addSubview:rewradBtn];
    [_countBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(resuableView.mas_centerY);
        make.top.equalTo(resuableView.mas_top).offset(scaleTopAndBottom(10));
        make.left.equalTo(_costLabel.mas_right).offset(scaleLeftAndRight(5));
    }];
    [rewradBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(resuableView.mas_centerY);
        make.left.equalTo(_countBtn.mas_right);
        make.top.equalTo(resuableView.mas_top).offset(scaleTopAndBottom(10));
        make.right.equalTo(resuableView.mas_right).offset(scaleLeftAndRight(-5));
        make.width.equalTo(_countBtn);
    }];
    iconImageView.backgroundColor = [UIColor yellowColor];
    
}

/**
 列表展示
 */
-(void)showCountList:(UIButton *)btn{
    CGPoint point=CGPointMake(btn.frame.origin.x + btn.frame.size.width/2, self.frame.origin.y + self.frame.size.height - scaleTopAndBottom(55));//箭头点的位置
    NSLog(@"%@",NSStringFromCGRect(self.countBtn.frame));
    WBPopOverView *view=[[WBPopOverView alloc]initWithOrigin:point Width:scaleLeftAndRight(200) Height:scaleTopAndBottom(310) Direction:WBArrowDirectionDown2];
    [view popView];
    __weak typeof(WBPopOverView *) weakView = view;
    view.countBlock = ^(NSString *counts) {
        [btn setTitle:counts forState:UIControlStateNormal];
        NSString *costStr = [_costLabel.text componentsSeparatedByString:@"元"][0];
        _costLabel.text = [NSString stringWithFormat:@"%ld",[costStr integerValue] * [counts integerValue]];
        [weakView dismiss];
    };
}
-(void)goPayChoose:(UIButton *)btn{
    payChooseView *chooseView = [[payChooseView alloc] initWithFrame:CGRectMake(0, HEIGHT, WIDTH, HEIGHT)];
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    [window addSubview:chooseView];
    [UIView animateWithDuration:1.0 animations:^{
        chooseView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    }];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    RewardCell *cell = (RewardCell *)[collectionView cellForItemAtIndexPath:indexPath];
    _costLabel.text = cell.nameLabel.text;
    NSLog(@"%@",_costLabel.text);
}


@end
